package com.spring.exeception;

import com.spring.common.web.CommRes;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;

@RestControllerAdvice
public class CommonExceptionHandler {
    //用于捕获@RequestBody类型参数触发校验规则抛出的异常
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public CommRes handleValidException(MethodArgumentNotValidException e) {
        StringBuilder sb = new StringBuilder();
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        if (!CollectionUtils.isEmpty(allErrors)) {
            for (ObjectError error : allErrors) {
                sb.append(error.getDefaultMessage()).append(";");
            }
        }
        return CommRes.fail(sb.toString());
    }
    //用于捕获@RequestParam/@PathVariable参数触发校验规则抛出的异常
    @ExceptionHandler(value = ConstraintViolationException.class)
    public CommRes handleConstraintViolationException(ConstraintViolationException e) {
        StringBuilder sb = new StringBuilder();
        Set<ConstraintViolation<?>> conSet = e.getConstraintViolations();
        for (ConstraintViolation<?> con : conSet) {
            String message = con.getMessage();
            sb.append(message).append(";");
        }
        return CommRes.fail(sb.toString());
    }
    @ExceptionHandler(value = BindException.class)
    public CommRes handleConstraintViolationException(BindException e) {
        StringBuilder sb =  new StringBuilder();
        List<ObjectError> allErrors = e.getAllErrors();
        for (ObjectError allError : allErrors) {
            String defaultMessage = allError.getDefaultMessage();
            sb.append(defaultMessage).append(";");
        }

        return CommRes.fail(sb.toString());
    }
    @ExceptionHandler(value = Exception.class)
    public CommRes exception(Exception e) {
        return CommRes.fail(e.getMessage());
    }
}
